﻿namespace AnonsaiWPF
{
    using System;
    using System.Data;
    using System.Windows;
    using System.Windows.Controls;
    using OutlookCalendar.Model;
    using System.Windows.Media;
    using System.Collections.Generic;
    using DevExpress.Xpf.Grid;

    internal class Functionality
    {
        public static int countrec = 100;
        public static int fromrec = 0;
        public static string CalendarDayOfWeek = "";
        public static Appointments appointments = new Appointments();
        public static Appointment appointmentSelected = null;
        public static DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");

        public static void ArchiveRecord(DataGrid dataGrid1, string searchword)
        {
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            for (int i = 0; i < dataGrid1.SelectedItems.Count; i++)
            {
                DataRowView li = (DataRowView) dataGrid1.SelectedItems[i];
                string idd = li.Row.ItemArray.GetValue(0x22).ToString();
                string sql = "insert into Archyvas$Archyve ([Kompanija],[Pavadinimas originalo kalba],[Lietuviškas pavadinimas],[Trukmė 1],[Trukmė 2],[TC trukmė],[Pradzios TC],[I Įsikirtimas],[II Įsikirtimas] ,[III Įsikirtimas],[IV Įsikirtimas],[V Įsikirtimas],[VI Įsikirtimas],[VII Įsikirtimas],[VIII Įsikirtimas],[IX Įsikirtimas],[X Įsikirtimas],[XI Įsikirtimas],[Eteris 1],[Eteris 2],[Eteris 3],[Eteris 4],[Eteris 5] ,[P raidė],[Intarpai bėg#eil],[Licencijos pradžia],[Licencijos pabaiga],žanras,Metai,Režisierius,Aktoriai,Sezonas,Serija,[XII Įsikirtimas],[XIII Įsikirtimas],[XIV Įsikirtimas],[XV Įsikirtimas],KasetesNr,Salis,indeksas) values ('" + li.Row.ItemArray.GetValue(0).ToString() + "','" + li.Row.ItemArray.GetValue(1).ToString() + "','" + li.Row.ItemArray.GetValue(2).ToString() + "','" + li.Row.ItemArray.GetValue(3).ToString() + "','" + li.Row.ItemArray.GetValue(4).ToString() + "','" + li.Row.ItemArray.GetValue(5).ToString() + "','" + li.Row.ItemArray.GetValue(6).ToString() + "','" + li.Row.ItemArray.GetValue(7).ToString() + "','" + li.Row.ItemArray.GetValue(8).ToString() + "','" + li.Row.ItemArray.GetValue(9).ToString() + "','" + li.Row.ItemArray.GetValue(10).ToString() + "','" + li.Row.ItemArray.GetValue(11).ToString() + "','" + li.Row.ItemArray.GetValue(12).ToString() + "','" + li.Row.ItemArray.GetValue(13).ToString() + "','" + li.Row.ItemArray.GetValue(14).ToString() + "','" + li.Row.ItemArray.GetValue(15).ToString() + "','" + li.Row.ItemArray.GetValue(16).ToString() + "','" + li.Row.ItemArray.GetValue(17).ToString() + "','" + li.Row.ItemArray.GetValue(18).ToString() + "','" + li.Row.ItemArray.GetValue(19).ToString() + "','" + li.Row.ItemArray.GetValue(20).ToString() + "','" + li.Row.ItemArray.GetValue(21).ToString() + "','" + li.Row.ItemArray.GetValue(22).ToString() + "','" + li.Row.ItemArray.GetValue(23).ToString() + "','" + li.Row.ItemArray.GetValue(24).ToString() + "','" + li.Row.ItemArray.GetValue(26).ToString() + "','" + li.Row.ItemArray.GetValue(27).ToString() + "','" + li.Row.ItemArray.GetValue(28).ToString() + "','" + li.Row.ItemArray.GetValue(29).ToString() + "','" + li.Row.ItemArray.GetValue(30).ToString() + "','" + li.Row.ItemArray.GetValue(31).ToString() + "','" + li.Row.ItemArray.GetValue(32).ToString() + "','" + li.Row.ItemArray.GetValue(33).ToString() + "','" + li.Row.ItemArray.GetValue(35).ToString() + "','" + li.Row.ItemArray.GetValue(36).ToString() + "','" + li.Row.ItemArray.GetValue(37).ToString() + "','" + li.Row.ItemArray.GetValue(38).ToString() + "','" + li.Row.ItemArray.GetValue(39).ToString() + "','" + li.Row.ItemArray.GetValue(40).ToString() + "','" + li.Row.ItemArray.GetValue(41).ToString() + "')";
                dbc.sqlExecute(sql);
                dbc.sqlExecute("delete from Archyvas$ where id=" + idd);
            }
            DataTable tb = dbc.FindStringInTableArch(searchword, fromrec, countrec);
            dataGrid1.DataContext = tb;
        }

        public static void DeleteFromDB(DataGrid dataGrid1, string searchword)
        {
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            for (int i = 0; i < dataGrid1.SelectedItems.Count; i++)
            {
                DataRowView li = (DataRowView) dataGrid1.SelectedItems[i];
                dbc.sqlExecute("delete from Archyvas$ where id=" + li.Row.ItemArray.GetValue(0x22).ToString());
            }
            DataTable tb = dbc.FindStringInTable(searchword, fromrec, countrec);
            dataGrid1.DataContext = tb;
        }

        public static void getData(DataGrid dataGrid1)
        {
            fromrec = 0;
            countrec = 100;
            DataTable tb = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem").getTable("select * from Archyvas$ order by Kompanija,[Pavadinimas originalo kalba] asc");
            dataGrid1.DataContext = tb;
        }

        public static void getDataArch(DataGrid dataGrid1)
        {
            fromrec = 0;
            countrec = 100;
            DataTable tb = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem").getTable("select top 50 * from Archyvas$Archyve order by Kompanija,[Pavadinimas originalo kalba] asc");
            dataGrid1.DataContext = tb;
        }

       
        public static void RestorefromArchive(DataGrid dataGrid1, string searchword)
        {
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            for (int i = 0; i < dataGrid1.SelectedItems.Count; i++)
            {
                DataRowView li = (DataRowView) dataGrid1.SelectedItems[i];
                string idd = li.Row.ItemArray.GetValue(0x22).ToString();
                string sql = "insert into Archyvas$ ([Kompanija],[Pavadinimas originalo kalba],[Lietuviškas pavadinimas],[Trukmė 1],[Trukmė 2],[TC trukmė],[Pradzios TC],[I Įsikirtimas],[II Įsikirtimas] ,[III Įsikirtimas],[IV Įsikirtimas],[V Įsikirtimas],[VI Įsikirtimas],[VII Įsikirtimas],[VIII Įsikirtimas],[IX Įsikirtimas],[X Įsikirtimas],[XI Įsikirtimas],[Eteris 1],[Eteris 2],[Eteris 3],[Eteris 4],[Eteris 5] ,[P raidė],[Intarpai bėg#eil],[Licencijos pradžia],[Licencijos pabaiga],žanras,Metai,Režisierius,Aktoriai,Sezonas,Serija,[XII Įsikirtimas],[XIII Įsikirtimas],[XIV Įsikirtimas],[XV Įsikirtimas],KasetesNr,Salis,indeksas) values ('" + li.Row.ItemArray.GetValue(0).ToString() + "','" + li.Row.ItemArray.GetValue(1).ToString() + "','" + li.Row.ItemArray.GetValue(2).ToString() + "','" + li.Row.ItemArray.GetValue(3).ToString() + "','" + li.Row.ItemArray.GetValue(4).ToString() + "','" + li.Row.ItemArray.GetValue(5).ToString() + "','" + li.Row.ItemArray.GetValue(6).ToString() + "','" + li.Row.ItemArray.GetValue(7).ToString() + "','" + li.Row.ItemArray.GetValue(8).ToString() + "','" + li.Row.ItemArray.GetValue(9).ToString() + "','" + li.Row.ItemArray.GetValue(10).ToString() + "','" + li.Row.ItemArray.GetValue(11).ToString() + "','" + li.Row.ItemArray.GetValue(12).ToString() + "','" + li.Row.ItemArray.GetValue(13).ToString() + "','" + li.Row.ItemArray.GetValue(14).ToString() + "','" + li.Row.ItemArray.GetValue(15).ToString() + "','" + li.Row.ItemArray.GetValue(16).ToString() + "','" + li.Row.ItemArray.GetValue(17).ToString() + "','" + li.Row.ItemArray.GetValue(18).ToString() + "','" + li.Row.ItemArray.GetValue(19).ToString() + "','" + li.Row.ItemArray.GetValue(20).ToString() + "','" + li.Row.ItemArray.GetValue(21).ToString() + "','" + li.Row.ItemArray.GetValue(22).ToString() + "','" + li.Row.ItemArray.GetValue(23).ToString() + "','" + li.Row.ItemArray.GetValue(24).ToString() + "','" + li.Row.ItemArray.GetValue(26).ToString() + "','" + li.Row.ItemArray.GetValue(27).ToString() + "','" + li.Row.ItemArray.GetValue(28).ToString() + "','" + li.Row.ItemArray.GetValue(29).ToString() + "','" + li.Row.ItemArray.GetValue(30).ToString() + "','" + li.Row.ItemArray.GetValue(31).ToString() + "','" + li.Row.ItemArray.GetValue(32).ToString() + "','" + li.Row.ItemArray.GetValue(33).ToString() + "','" + li.Row.ItemArray.GetValue(35).ToString() + "','" + li.Row.ItemArray.GetValue(36).ToString() + "','" + li.Row.ItemArray.GetValue(37).ToString() + "','" + li.Row.ItemArray.GetValue(38).ToString() + "','" + li.Row.ItemArray.GetValue(39).ToString() + "','" + li.Row.ItemArray.GetValue(40).ToString() + "','" + li.Row.ItemArray.GetValue(41).ToString() + "')";
                dbc.sqlExecute(sql);
                dbc.sqlExecute("delete from Archyvas$Archyve  where id=" + idd);
            }
            DataTable tb = dbc.FindStringInTableArch(searchword, fromrec, countrec);
            dataGrid1.DataContext = tb;
        }

        public static void SearchInDB(DataGrid dataGrid1, string searchword)
        {
            DataTable tb = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem").FindStringInTable(searchword, fromrec, countrec);
            dataGrid1.DataContext = tb;
        }

        public static void SearchInDBArch(DataGrid dataGrid1, string searchword)
        {
            DataTable tb = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem").FindStringInTableArch(searchword, fromrec, countrec);
            dataGrid1.DataContext = tb;
        }

       

        public static void LoadAppointmentsByDate(DateTime StartDate, DateTime EndDate)
        {
            EndDate += TimeSpan.FromDays(1);
            EndDate = new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, 0, 0, 0);
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            DataTable tb = dbc.getTable("select * from Programa where StartDate>='" + StartDate + "' and EndDate<'" + EndDate + "' order by StartDate");
            appointments.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                Appointment ap = new Appointment();
                ap.Subject=tb.Rows[i]["Subject"].ToString();
                ap.StartTime=(DateTime)tb.Rows[i]["StartDate"];
                ap.EndTime = (DateTime)tb.Rows[i]["EndDate"];
                ap.Location = tb.Rows[i]["subID"].ToString();
                appointments.Add(ap);
            }
            try
                {
                    for (int y = 0; y <= appointments.Count; y++)
                    {
                        for (int i = appointments.Count-1; i >= 0; i--)
                        {
                            try
                            {
                                Appointment lastAp = appointments[i];
                                Appointment beforelastAp = appointments[i+1];
                                if ((lastAp.StartTime.Hour < 6) && (beforelastAp.StartTime.Hour >= 6) && lastAp.StartTime.ToShortDateString().Equals(beforelastAp.StartTime.ToShortDateString()))
                                {
                                    Appointment tmp = appointments[i];
                                    appointments[i] = appointments[i + 1];
                                    appointments[i + 1] = tmp;
                                }
                            }
                            catch (Exception ex) { }

                        }
                    }
                }
            catch (Exception ex) { }
        }

        public static void addAppointmentToDB(Appointment ap)
        {
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            string sql = "insert into Programa (Subject,StartDate,EndDate,subID) values ('" + ap.Subject.Replace("\r\n", "") + "','" + ap.StartTime + "','" + ap.EndTime + "','" + ap.Location + "')";
            dbc.sqlExecute(sql);
        }
        public static void removeAppointmentFromDB(Appointment ap)
        {
            DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
            string sub = ap.Subject.Replace("\r\n", "");
            string sqlc = "delete from Programa where StartDate='" + ap.StartTime + "' and EndDate='" + ap.EndTime + "'";
           // string sqlc = "delete from Programa where Subject='" + sub + "' and StartDate='" + ap.StartTime + "' and EndDate='" + ap.EndTime + "'";
            dbc.sqlExecute(sqlc);
        }

        public static DateTime GetFirstDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay)
        {
            DateTime firstDayInWeek = dayInWeek.Date;
            while (firstDayInWeek.DayOfWeek != firstDay)
                firstDayInWeek = firstDayInWeek.AddDays(-1);

            return firstDayInWeek;
        }
        public static DateTime GetLastDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay)
        {
            DateTime lastDayInWeek = dayInWeek.Date;
            while (lastDayInWeek.DayOfWeek != firstDay)
                lastDayInWeek = lastDayInWeek.AddDays(1);

            lastDayInWeek -= TimeSpan.FromDays(1);
            return lastDayInWeek;
        }

        public static DataTable getDataFromArchToCombo()
        {
             DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
             DataTable tb = dbc.getTable("select [Pavadinimas originalo kalba],[Lietuviškas pavadinimas],id from Archyvas$ where ([Pavadinimas originalo kalba] is not NULL and [Lietuviškas pavadinimas] is not NULL) and [Lietuviškas pavadinimas] <>'' order by dbo.fFilterNumeric([Lietuviškas pavadinimas])");
            return tb;
        }

        public static string removeNr(string str)
        {
            string rez =str;
            int ln = str.Length - 1;
            int cnt = 0;
            for (int i = ln; i > 0; i--)
            {
                try{
                    if (str.Substring(i, 1).Equals("/"))
                    {
                        rez = str.Substring(0, i);
                        ln--;
                        cnt++;
                    }else if ((int.Parse(str.Substring(i, 1)) >= 0) && (int.Parse(str.Substring(i, 1)) <= 9))
                    {
                    rez = str.Substring(0, i);
                    ln--;
                    cnt++;
                }
                }catch(Exception ex){ break;}
            }
            if (cnt>0){
                rez = rez.Substring(0,rez.Length - 1);
            }
            return rez;
        }

        public static string getProgramDateFromDB(string id,string Subject)
        {
            string result = "";
            try
            {
                if (!id.Equals("") && !id.Equals("null"))
                {
                    DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
                    DataTable tb = dbc.getTable("select * from Archyvas$ where id=" + id);

                    string priedas = "";
                    if (Subject.Contains("(tęs.)"))
                    {
                        priedas = " (tęs.)";
                    }
                    if (Subject.Contains("(kart.)"))
                    {
                        priedas = " (kart.)";
                    }
                    if (!tb.Rows[0]["Žanras"].ToString().Equals(""))
                    {
                        if (tb.Rows[0]["Lietuviškas pavadinimas"].ToString().Equals(tb.Rows[0]["Pavadinimas originalo kalba"].ToString()))
                        {
                            result += " " + removeNr(tb.Rows[0]["Lietuviškas pavadinimas"].ToString()) + priedas + ". " + tb.Rows[0]["Žanras"].ToString();
                        }
                        else
                        {
                            string skirt = ".";
                            if (tb.Rows[0]["Žanras"].ToString().Substring(tb.Rows[0]["Žanras"].ToString().Length - 1).Contains("."))
                                skirt = "";
                            string pav = removeNr(tb.Rows[0]["Lietuviškas pavadinimas"].ToString());
                            if (pav.Contains("("))
                            {
                                pav = pav.Replace(" (", "“ (");
                            }
                            else
                            {
                                pav = pav + "“";
                            }
                            result += " " + "„" + pav + priedas + ". " + tb.Rows[0]["Žanras"].ToString() + skirt;

                        }
                    }
                    else
                    {
                        result += " " + removeNr(tb.Rows[0]["Lietuviškas pavadinimas"].ToString()) + priedas;
                    }
                    if (!tb.Rows[0]["Režisierius"].ToString().Equals(""))
                    {
                        result += ". " + tb.Rows[0]["Režisierius"].ToString();
                    }
                    if (!tb.Rows[0]["Aktoriai"].ToString().Equals(""))
                    {
                        result += ". " + tb.Rows[0]["Aktoriai"].ToString();
                    }
                    if (!tb.Rows[0]["Salis"].ToString().Equals(""))
                    {
                        result += ". " + tb.Rows[0]["Salis"].ToString();
                    }
                    if (!tb.Rows[0]["Metai"].ToString().Equals(""))
                    {
                        result += ". " + tb.Rows[0]["Metai"].ToString();
                    }
                    if (!tb.Rows[0]["indeksas"].ToString().Equals(""))
                    {
                        result += ". " + tb.Rows[0]["indeksas"].ToString().Replace("(", "").Replace(")", "");
                    }
                    result = result.Replace("..", ".");
                }
            }
            catch (Exception ex) { }
            return result;
        }

        public static void AddClip(string name, string duration)
        {
            string sql = "insert into AnonsaiClips (ClipName,Duration) values ('" + name + "','" + duration + "')";
            dbc.sqlExecute(sql);
        }

        public static void EditClip(string id,string name, string duration)
        {
            string sql = "update AnonsaiClips set ClipName='" + name + "', Duration='" + duration + "' where id=" + id;
            dbc.sqlExecute(sql);
        }

        public static void deleteClip(string id)
        {
            string sql = "delete from AnonsaiClips where id=" + id;
            dbc.sqlExecute(sql);
        }

        public static void AddAnons(string name, string clipid)
        {

            string sql = "insert into Anonsai (Name,ClipId) values ('" + name + "'," + clipid + ")";
            dbc.sqlExecute(sql);
        }

        public static void deleteAnons(string id)
        {
            string sql = "delete from Anonsai where id=" + id;
            dbc.sqlExecute(sql);
        }

        public static void EditAnons(string id, string name, string Clipid)
        {

            string sql = "update Anonsai set Name='" + name + "', ClipId=" + Clipid + " where id=" + id;
            dbc.sqlExecute(sql);
        }


        public static void serachClips(GridControl grid,string sword)
        {
            DataTable tb = dbc.getTable("select * from AnonsaiClips where ClipName like '%" + sword + "%'");
            grid.DataSource = tb;
        }

        public static void serachAnons(GridControl grid, string sword)
        {
            DataTable tb = dbc.getTable("select * from Anonsai an left join AnonsaiClips anc on an.ClipId=anc.id where Name like '%" + sword + "%'");
            grid.DataSource = tb;
        }

        public static void getBlocks(GridControl grid, string dayofweek)
        {
            DataTable tb = dbc.getTable("select * from BlockAnons where WeekDay like '%" + dayofweek + "%'");
            grid.DataSource = tb;
        }

        public static string getDurationByAnonsId(string id)
        {
            DataTable tb = dbc.getTable("select * from Anonsai a left join AnonsaiClips ac on ClipId=ac.Id where a.Id=" + id);
            if (tb.Rows.Count > 0) { return tb.Rows[0]["Duration"].ToString(); }
            else { return "00:00:00"; }
        }

        public static string getNameByAnonsId(string id)
        {
            DataTable tb = dbc.getTable("select * from Anonsai a left join AnonsaiClips ac on ClipId=ac.Id where a.Id=" + id);
            if (tb.Rows.Count > 0) { return tb.Rows[0]["Name"].ToString(); }
            else { return ""; }
        }

        public static bool BlockByDateExists(string date)
        {
            bool rez = false;
            DataTable tb = dbc.getTable("select * from BlockAnons where WeekDay='" + date + "'");
            if (tb.Rows.Count > 0) { rez = true; }
            return rez;
        }

        public static void saveBlocks(GridControl grid, string dayofweek)
        {
            dbc.sqlExecute("delete from BlockAnons where WeekDay='" + dayofweek + "'");
            DataTable tb = (DataTable)grid.DataSource;
           // string weekdays="";
           // string dayweek=dayofweek;
           // if !weekdays.Contains(dayofweek){
           //         dayweek=dayofweek.Substring(0,10)
           // }
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string sql = "insert into BlockAnons (BlockNr,AnonsTime,ProgName,BlockDuration,Rait,WeekDay) values (" + tb.Rows[i]["BlockNr"].ToString() + ",'" + tb.Rows[i]["AnonsTime"].ToString() + "','" + tb.Rows[i]["ProgName"].ToString() + "','" + tb.Rows[i]["BlockDuration"].ToString() + "','" + tb.Rows[i]["Rait"].ToString().Replace(",",".") + "','"+dayofweek+"')";
                dbc.sqlExecute(sql); 
            }
            //DataTable tb = dbc.getTable("select * from BlockAnons where WeekDay like '%" + dayofweek + "%'");
            //grid.DataSource = tb;
        }

        public static void saveAutoPlanToDb(DataTable tb,string date)
        {
            dbc.sqlExecute("delete from AutoPlan where DateTime='" + date + "'");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string sql = "insert into AutoPlan (AnonsId,Amount,DateTime) values (" + tb.Rows[i]["AnonsId"].ToString() + "," + tb.Rows[i]["Amount"].ToString() + ",'" + date+ "')";
                dbc.sqlExecute(sql);
            }
        }


        public static string getTinklelismDateFromDB(string id,string Subject)
        {
            string result = "";
            if (!id.Equals("") && !id.Equals("null"))
            {
                string priedas = "";
                if (Subject.Contains("(tęs.)"))
                {
                    priedas = " (tęs.)";
                }
                if (Subject.Contains("(kart.)"))
                {
                    priedas = " (kart.)";
                }
                DataBase dbc = new DataBase(@"192.168.100.100\SQLEXPRESS", "sa", "tornado", "PASystem");
                DataTable tb = dbc.getTable("select * from Archyvas$ where id=" + id);
               
                    if (!tb.Rows[0]["Lietuviškas pavadinimas"].ToString().Equals(tb.Rows[0]["Pavadinimas originalo kalba"].ToString()))
                    {
                        result = tb.Rows[0]["Lietuviškas pavadinimas"].ToString() + priedas+ ".   " + tb.Rows[0]["Pavadinimas originalo kalba"].ToString() + ". " + tb.Rows[0]["Kompanija"].ToString();
                    }
                    else
                    {
                        result = tb.Rows[0]["Pavadinimas originalo kalba"].ToString()+priedas;
                    }
                
                
            }
            return result;
        }

        public static void getAutoPlan(DataGrid grid, string date)
        {
            DataTable tb = dbc.getTable("select * from AutoPlan where DateTime='" + date + "'");
            grid.ItemsSource = tb.AsDataView();
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    
    
    }
}

